package cn.edu.uibe.ui_demo.activity;

import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;


import java.util.ArrayList;
import java.util.List;

import cn.edu.uibe.ui_demo.Fragment.CollectionFragment;
import cn.edu.uibe.ui_demo.Fragment.SearchFragment;
import cn.edu.uibe.ui_demo.Fragment.UserFragment;
import cn.edu.uibe.ui_demo.Fragment.shopFragment;
import cn.edu.uibe.ui_demo.R;

public class HomeActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, BottomNavigationView.OnNavigationItemSelectedListener {
    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;
    private FragmentPagerAdapter adapter;
    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_home );

        viewPager = findViewById( R.id.viewPager );
        bottomNavigationView = findViewById( R.id.bottomNavigationView );
        fragmentList = new ArrayList<>();
        fragmentList.add( new shopFragment() );
        fragmentList.add( new CollectionFragment() );
        fragmentList.add( new SearchFragment() );
        fragmentList.add( new UserFragment() );
        adapter = new FragmentPagerAdapter( getSupportFragmentManager() ) {
            @Override
            public Fragment getItem(int i) {
// 根据位置返回对应的 Fragment
// 0 对应 ShopFragment ， 1 对应 CollectionFragment 。。
                return fragmentList.get( i );
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter( adapter );
        viewPager.addOnPageChangeListener(this);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        bottomNavigationView.setSelectedItemId( R.id.nav_shop );
    }

    public boolean onNavigationItemSelected(MenuItem menuItem) {
        int id = 0;
        switch (menuItem.getItemId())
        {
            case R.id.nav_shop:
                id=0;
                break;
            case R.id.nav_collection:
                id=1;
                break;
            case R.id.nav_search:
                id=2;
                break;
            case R.id.nav_user:
                id=3;
                break;
        }
        viewPager.setCurrentItem( id );
        return true;
    }
    public void onPageScrolled(int i,float v,int i1)
    {

    }
    public void onPageSelected(int i)
    {
        int id=0;
        switch (i){
            case 0:
                id=R.id.nav_shop;
                break;
            case 1:
                id=R.id.nav_collection;
                break;
            case 2:
                id=R.id.nav_search;
                break;
            case 3:
                id=R.id.nav_user;
                break;
        }
        bottomNavigationView.setSelectedItemId( id );
    }
    public void onPageScrollStateChanged(int i)
    {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
